package agency.tango.materialintroscreen.adapter;

import java.util.ArrayList;

import agency.tango.materialintroscreen.SlideComponentContainer;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;
import ohos.data.rdb.DataObservable;
import ohos.data.rdb.DataObserver;

import agency.tango.materialintroscreen.SlideViewBuilder;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintroscreen.adapter
 * @ClassName: SlidesAdapter
 * @Description: pageslider的适配器
 * @CreateDate: 2021/4/12 15:34
 * @UpdateDate: 2021/4/12 15:34
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SlidesAdapter extends PageSliderProvider {
    private final DataObservable mObservable = new DataObservable();
    private DataObserver mViewPagerObserver;
    private ArrayList<SlideComponentContainer> views = new ArrayList<>();

    /**
     * 构造器
     */
    public SlidesAdapter() {
    }

    /**
     * 设置pageslider数据变化观察者
     *
     * @param mViewPagerObserver 数据变化观察者
     */
    public void setViewPagerObserver(DataObserver mViewPagerObserver) {
        this.mViewPagerObserver = mViewPagerObserver;
    }

    public void setViews(ArrayList<SlideComponentContainer> views) {
        this.views = views;
    }

    @Override
    public int getCount() {
        return views == null ? 0 : views.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        SlideComponentContainer slideComponet;
        ComponentContainer.LayoutConfig layoutParams =
                new ComponentContainer.LayoutConfig(
                        ComponentContainer.LayoutConfig.MATCH_PARENT,
                        ComponentContainer.LayoutConfig.MATCH_PARENT);
        if (this.views.size() > position) {
            slideComponet = this.views.get(position);
            if (slideComponet != null && slideComponet.getRootView() != null) {
                componentContainer.addComponent(slideComponet.getRootView(),
                        layoutParams);
                views.set(position, slideComponet);
                return slideComponet.getRootView();
            }
        }
        slideComponet = this.getItem(position);
        while (this.views.size() <= position) {
            this.views.add(null);
        }
        this.views.set(position, slideComponet);
        if (slideComponet != null && slideComponet.getRootView() != null) {
            componentContainer.addComponent(slideComponet.getRootView(),
                    layoutParams);
            views.set(position, slideComponet);
            return slideComponet.getRootView();
        } else {
            return new Component(componentContainer.getContext());
        }
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object obj) {
        if (obj instanceof Component && componentContainer != null) {
            componentContainer.removeComponent((Component) obj);
        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object obj) {
        return component == obj;
    }


    @Override
    public String getPageTitle(int position) {
        return views.get(position).getTitle();
    }

    /**
     * 判断是否是最后一个
     *
     * @param position pageslider子控件位置
     * @return 是否是最后一个
     */
    public boolean isLastSlide(int position) {
        return position == getCount() - 1;
    }

    /**
     * 判断是否应该关闭页面
     *
     * @param position pageslider子控件位置
     * @return 是否应该关闭页面
     */
    public boolean shouldFinish(int position) {
        return position == getCount() && getItem(getCount() - 1).canMoveFurther();
    }

    /**
     * 判断是否应该锁定页面 禁止pageslider滑动
     *
     * @param position pageslider子控件位置
     * @return 是否应该锁定页面
     */
    public boolean shouldLockSlide(int position) {
        SlideComponentContainer slideComponet = getItem(position);
        return !slideComponet.canMoveFurther() || slideComponet.hasNeededPermissionsToGrant();
    }

    /**
     * 获取某个位置的子控件
     *
     * @param var1 pageslider子控件位置
     * @return 某个位置的子控件
     */
    public SlideComponentContainer getItem(int var1) {
        if (views == null || var1 < 0 || var1 >= views.size() || views.get(var1) == null) {
            return new SlideViewBuilder().build(null);
        }

        return views.get(var1);
    }

    /**
     * pageslider适配器添加子控件
     *
     * @param view 子控件
     */
    public void addItem(SlideComponentContainer view) {
        views.add(getCount(), view);
        notifyDataChanged();
    }

    /**
     * 获取pageslider最后一个子控件的位置
     *
     * @return 最后一个子控件的位置
     */
    public int getLastItemPosition() {
        return Math.max(getCount() - 1, 0);
    }

    @Override
    public void notifyDataChanged() {
        super.notifyDataChanged();
        synchronized (this) {
            if (this.mViewPagerObserver != null) {
                this.mViewPagerObserver.onChange();
            }
        }

        this.mObservable.notifyObservers();
    }

    /**
     * 注册适配器数据更新观察者
     *
     * @param observer 数据观察者
     */
    public void registerDataSetObserver(DataObserver observer) {
        this.mObservable.add(observer);
    }

    /**
     * 注销适配器数据更新观察者
     *
     * @param observer 数据观察者
     */
    public void unregisterDataSetObserver(DataObserver observer) {
        this.mObservable.remove(observer);
    }
}
